var drawApp = true;
var defaultFill = "#fffdc9";
var defaultColor = "#000";
var defaultLineWidth = 12;
var brushImage = null;
var cursorOffset = 6;
var painted = false;
var restorePoints = [];
$(".colorPicker").click(function(a) {
	$(".colorPicker").removeClass("selected");
	$(this).addClass("selected");
	a.preventDefault()
});
$(".brushPicker").click(function(a) {
	$(".brushPicker").removeClass("selected");
	$(this).addClass("selected");
	a.preventDefault()
});
function DrawApp(a) {
	this.renderFunction = this.updateCanvasByLine;
	this.touchSupported = Modernizr.touch;
	this.canvasID = a;
	this.canvas = $("#" + a);
	this.context = this.canvas.get(0).getContext("2d");
	this.context.fillStyle = "#fffdc9";
	this.context.fillRect(0, 0, this.context.canvas.width,
			this.context.canvas.height);
	this.context.strokeStyle = defaultColor;
	this.context.lineWidth = defaultLineWidth;
	this.context.lineCap = "round";
	this.lastMousePoint = {
		x : 0,
		y : 0
	};
	if (this.touchSupported) {
		this.mouseDownEvent = "touchstart";
		this.mouseMoveEvent = "touchmove";
		this.mouseUpEvent = "touchend"
	} else {
		this.mouseDownEvent = "mousedown";
		this.mouseMoveEvent = "mousemove";
		this.mouseUpEvent = "mouseup"
	}
	this.context.fillText = this.context.strokeText = this.context.putImageData = this.context.strokeRect = this.context.clearRect = function() {
	};
	this.canvas.bind(this.mouseDownEvent, this.onCanvasMouseDown());
	this.canvas.bind("mousedown", handleMouseDown, false)
}
DrawApp.prototype.onCanvasMouseDown = function() {
	var a = this;
	var b = 1;
	return function(c) {
		if (a.touchSupported) {
			b = c.originalEvent.touches.length
		}
		if (b == 1) {
			save();
			a.mouseMoveHandler = a.onCanvasMouseMove();
			a.mouseUpHandler = a.onCanvasMouseUp();
			$(document).bind(a.mouseMoveEvent, a.mouseMoveHandler);
			$(document).bind(a.mouseUpEvent, a.mouseUpHandler);
			a.updateMousePosition(c);
			a.renderFunction(c);
			painted = true
		} else {
			a.onCanvasMouseUp
		}
	}
};
DrawApp.prototype.onCanvasMouseMove = function() {
	var a = this;
	var b = 1;
	return function(c) {
		if (a.touchSupported) {
			b = c.originalEvent.touches.length
		}
		if (b == 1) {
			a.renderFunction(c);
			c.preventDefault();
			return false
		}
	}
};
DrawApp.prototype.onCanvasMouseUp = function(b) {
	var a = this;
	return function(c) {
		$(document).unbind(a.mouseMoveEvent, a.mouseMoveHandler);
		$(document).unbind(a.mouseUpEvent, a.mouseUpHandler);
		a.mouseMoveHandler = null;
		a.mouseUpHandler = null
	}
};
DrawApp.prototype.updateMousePosition = function(a) {
	var b;
	if (this.touchSupported) {
		b = a.originalEvent.touches[0]
	} else {
		b = a
	}
	var c = this.canvas.offset();
	this.lastMousePoint.x = b.pageX - c.left;
	this.lastMousePoint.y = b.pageY - c.top
};
DrawApp.prototype.updateCanvasByLine = function(a) {
	this.context.beginPath();
	this.context.moveTo(this.lastMousePoint.x, this.lastMousePoint.y);
	this.updateMousePosition(a);
	this.context.lineTo(this.lastMousePoint.x, this.lastMousePoint.y + 0.001);
	this.context.stroke()
};
DrawApp.prototype.toString = function() {
	var b = this.canvas.get(0).toDataURL("image/png");
	var a = b.indexOf(",") + 1;
	b = b.substring(a);
	return b
};
DrawApp.prototype.toDataURL = function() {
	var a = this.canvas.get(0).toDataURL("image/png");
	return a
};
DrawApp.prototype.exportImage = function(b) {
	var a = new Image();
	a.src = this.toDataURL();
	a.onload = function() {
		var e = document.getElementById("tempCanvas");
		var d = e.getContext("2d");
		d.drawImage(a, 0, 0, 300, 250);
		var c = e.toDataURL("image/png");
		$("#newimg").html($("<img src=" + c + " width=300 height=250 />"))
	}
};
DrawApp.prototype.reset = function() {
	apprise("Erase the drawing?", {
		verify : true
	}, function(a) {
		if (a) {
			cv = $("#drawingCanvas");
			ctx = cv.get(0).getContext("2d");
			ctx.fillStyle = "#fffdc9";
			ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
			restorePoints = []
		}
	});
	return false
};
DrawApp.prototype.setColor = function(a) {
	this.context.strokeStyle = a;
	event.preventDefault()
};
DrawApp.prototype.setSize = function(a) {
	this.context.lineWidth = a;
	cursorOffset = a / 2;
	event.preventDefault()
};
$("#drawingCanvas").bind("contextmenu", function(a) {
	return false
});
function save() {
	var b = document.getElementById("drawingCanvas");
	var a = b.toDataURL("image/png");
	restorePoints.push(a)
}
function undo() {
	if (restorePoints.length > 0) {
		var a = new Image();
		a.onload = function() {
			var b = document.getElementById("drawingCanvas").getContext("2d");
			b.drawImage(a, 0, 0)
		};
		a.src = restorePoints.pop()
	}
}
function handleMouseDown(a) {
	a.preventDefault();
	a.stopPropagation();
	a.target.style.cursor = "crosshair"
};